package org.jeecg.modules.fund.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.fund.entity.FundHold;
import org.jeecg.modules.fund.entity.FundLibrary;
import org.jeecg.modules.fund.entity.FundOptional;
import org.jeecg.modules.fund.entity.FundTrade;
import org.jeecg.modules.fund.mapper.FundHoldMapper;
import org.jeecg.modules.fund.mapper.FundTradeMapper;
import org.jeecg.modules.fund.service.IFundHoldService;
import org.jeecg.modules.fund.service.IFundLibraryService;
import org.jeecg.modules.fund.service.IFundTradeService;
import org.jeecg.modules.fund.service.IFundValueService;
import org.jeecg.modules.system.entity.SysCategory;
import org.jeecg.modules.system.entity.SysDataLog;
import org.jeecg.modules.system.entity.SysRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "持有管理")
@RequestMapping("/sys/fund/trade")
@Slf4j
public class FundTradeController {
    @Autowired
    private IFundTradeService fundTradeService;
    @Autowired
    private IFundLibraryService fundLibraryService;
    @Autowired
    private IFundValueService fundValueService;
    @Autowired
    private FundTradeMapper fundTradeMapper;
    @Autowired
    private IFundHoldService fundHoldService;

    /**
     * 添加持有
     *
     * @param trade
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public Result<FundTrade> create(@RequestBody FundTrade trade) {
        return fundTradeService.createTrade(trade);
    }

    /**
     * 分页
     *
     * @param fundTrade
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    public Result<IPage<FundTrade>> page(FundTrade fundTrade,
                                         @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                         HttpServletRequest req) {
        Result<IPage<FundTrade>> result = new Result<IPage<FundTrade>>();
        QueryWrapper<FundTrade> queryWrapper = QueryGenerator.initQueryWrapper(fundTrade, req.getParameterMap());
        Page<FundTrade> page = new Page<FundTrade>(pageNo, pageSize);
        IPage<FundTrade> pageList = fundTradeService.page(page, queryWrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
     * 获取趋势线的值(自选)
     *
     * @param holdId
     * @param str
     * @return
     */
    @RequestMapping(value = "/queryLine", method = RequestMethod.GET)
    public Result<List<Map<String, Object>>> queryLine(@RequestParam(name = "holdId", required = true) String holdId,
                                                       @RequestParam(name = "str", required = true) String str) {
        FundHold hold = fundHoldService.getById(holdId);
        if (hold == null) {
            return Result.error("持有数据不存在");
        }
        // 起始日期
        String startDate;
        // 获取表名
        String tableName = fundValueService.tableName(hold.getFundId().substring(5));
        // 格式化日期
        if ("近1月".equals(str)) {
            startDate = DateUtil.format(DateUtil.offsetDay(new Date(), -29), "yyyy-MM-dd");
        } else if ("近3月".equals(str)) {
            startDate = DateUtil.format(DateUtil.offsetDay(new Date(), -89), "yyyy-MM-dd");
        } else if ("近6月".equals(str)) {
            startDate = DateUtil.format(DateUtil.offsetDay(new Date(), -179), "yyyy-MM-dd");
        } else if ("近1年".equals(str)) {
            startDate = DateUtil.format(DateUtil.offsetDay(new Date(), -365), "yyyy-MM-dd");
        } else if ("近3年".equals(str)) {
            startDate = DateUtil.format(DateUtil.offsetDay(new Date(), -1095), "yyyy-MM-dd");
        } else if ("近5年".equals(str)) {
            startDate = DateUtil.format(DateUtil.offsetDay(new Date(), -1825), "yyyy-MM-dd");
        } else if ("今年来".equals(str)) {
            startDate = DateUtil.format(new Date(), "yyyy") + "-01-01";
        } else {
            // 成立来
            startDate = null;
        }
        // 结果集
        List<Map<String, Object>> data = fundTradeMapper.findTradeValueList(tableName,holdId,startDate, DateUtil.format(new Date(),"yyyy-MM-dd"));
        return Result.OK(data);
    }
}
